﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddShatterItem : FormTemplet
    {
        private long _nShatterID = -1L;

        private FormShatterInfo frmShatterInfo;

        private FormAddShatterItem()
        {
            this.InitializeComponent();
        }

        public FormAddShatterItem(long nShatterID, FormShatterInfo frmShatterInfo) : this()
        {
            this._nShatterID = nShatterID;
            this.frmShatterInfo = frmShatterInfo;
        }

        private void FormAddOrderItem_Activated(object sender, EventArgs e)
        {
            base.WindowState = FormWindowState.Normal;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtProductID.Text.Trim().Length < 1)
            {
                MessageBox.Show("你输入的号码格式不能为空，格式请参照产品ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtProductID.Focus();
            }
            else if (this.txtProductID.Focused)
            {
                this.cbTransitionThickness.Focus();
            }
            else if (this.cbTransitionThickness.Text.Trim().Length > 64 || this.cbTransitionThickness.Text.Trim().Length < 1)
            {
                MessageBox.Show("补片层名称 格式不正确，为空或者字符太长，应小于等于64个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbTransitionThickness.Focus();
            }
            else if (this.cbTransitionThickness.Focused)
            {
                this.txtQuantity.Focus();
            }
            else if (!Regex.IsMatch(this.txtQuantity.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtQuantity.Text.Trim()) <= 0)
            {
                MessageBox.Show("数量 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtQuantity.Focus();
            }
            else if (this.txtQuantity.Focused)
            {
                this.cbDepartment.Focus();
            }
            else if (this.cbDepartment.Text.Trim().Length > 32 || this.cbDepartment.Text.Trim().Length < 1)
            {
                MessageBox.Show("请选择工作组", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbDepartment.Focus();
            }
            else if (this.cbDepartment.Focused)
            {
                this.txtReason.Focus();
            }
            else if (this.txtReason.Text.Trim().Length > 32)
            {
                MessageBox.Show("补片原因 格式不正确，字符太长，应小于等于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtReason.Focus();
            }
            else
            {
                ShatterItems shatterItems = new ShatterItems();
                bool flag = false;
                try
                {
                    flag = shatterItems.InsertShatterItem(this._nShatterID, this.txtProductID.Text.Trim(), this.cbTransitionThickness.Text.Trim(), Convert.ToInt32(this.txtQuantity.Text.Trim()), this.cbDepartment.Text.Trim(), this.txtReason.Text.Trim(), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number <= 50001)
                    {
                        if (number == 2627)
                        {
                            MessageBox.Show("此补片单已经有此玻璃的补片了，不要重复补片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_3BC;
                        }
                        if (number == 50001)
                        {
                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_3BC;
                        }
                    }
                    else
                    {
                        if (number == 50200)
                        {
                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_3BC;
                        }
                        switch (number)
                        {
                            case 50500:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                goto IL_3BC;
                            case 50501:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.frmShatterInfo.chkLockedData.Checked = true;
                                goto IL_3BC;
                        }
                    }
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                IL_3BC:;
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                shatterItems.Dispose();
                if (flag)
                {
                    MessageBox.Show("补片 录入成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.frmShatterInfo.ReloadItemForInsert(this.txtProductID.Text.Trim());
                    this.txtQuantity.Text = "";
                    this.txtReason.Text = "";
                    this.txtProductID.Focus();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
